package com.demo.netty;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;

@Slf4j
public class NettyPromiseDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        EventLoop loop = new NioEventLoopGroup().next();
        DefaultPromise<Integer> promise = new DefaultPromise<>(loop);
        new Thread(() -> {
            log.info("thread inside");
            try {
                Thread.sleep(100);
                //在这个thread 中，主动向promise添加结果
                promise.setSuccess(1);
            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
                //优点在于 在main 处理 thread 中的异常
                promise.setFailure(e);
            }
        }).start();

        log.info("thread outside");
        log.info("result:{}", promise.get());
    }
}
